Attribute VB_Name = "modMenuPPal"
Option Explicit

Global mUser As String
Global mPsw As String

Private Declare Function ShowWindow Lib "user32" _
                (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" _
                Alias "FindWindowA" _
                (ByVal lpClassName As String, ByVal lpWindowName _
                As String) As Long
Private Declare Function IsIconic Lib "user32" _
                (ByVal hwnd As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" _
                (ByVal hwnd As Long) As Long

Const SW_RESTORE = 9
'Conatants used for the return of the MultiInst function
Private Const OPEN_APPLICATION = 0
Private Const SINGLE_INSTANCE_OPEN = 1
Public Function VerificarAplicacionAbierta(ByVal titulo As String) As Boolean
Dim AppOpen As Integer

'Call procedure to determine if an instance of
'the application is already loaded
AppOpen = MultiInst(titulo)
'Handle the result from the MultiInst function
If AppOpen = OPEN_APPLICATION Then
    VerificarAplicacionAbierta = False
ElseIf AppOpen = SINGLE_INSTANCE_OPEN Then
    VerificarAplicacionAbierta = True
End If

End Function

Private Function MultiInst(ByVal titulo As String) As Integer
'This function determines if a single instance of the 'application is already running.
Dim hwndFound As Long   'The window handle
Dim strWindowName       'The Caption on the window

'Set the caption of the application form
strWindowName = titulo
'Get the handle of the application if it is open
hwndFound = FindWindow(vbNullString, strWindowName)

If hwndFound Then
    'Set the function return
    MultiInst = SINGLE_INSTANCE_OPEN
    MsgBox "A instance of the application is already open." & _
    vbCrLf & vbCrLf & _
    "Only one open instance allowed.", vbOKOnly + _
    vbExclamation, "App Name"
    'If application minimized, restore, show it on top
    If IsIconic(hwndFound) Then
        ShowWindow hwndFound, SW_RESTORE
        'Show the window infront of all other windows
        SetForegroundWindow hwndFound
    Else
        'Bring the application top most on the screen
        SetForegroundWindow hwndFound
    End If
ElseIf hwndFound = 0 Then
    'Set the function return so it will continue loading
    MultiInst = OPEN_APPLICATION
End If

End Function


